layui.use('table', function(){
  var table = layui.table;
  var delurl = "/general/erp5/public/tripapi/plan/Delete";
  //方法级渲染
  table.render({
    elem: '#apply_list'
    ,url: '/general/erp5/public/tripapi/plan/GetListByAjax'
    ,response: {
        statusName: 'status'
        ,statusCode: 200
        ,msgName: 'msg'
        ,countName: 'count'
        ,dataName: 'data'
      }
    ,cols: [[
       {checkbox: true, fixed: true}
      ,{field:'id', title: 'ID',width:70}
      ,{field:'plan_num', title: '计划单号',width: 150}
      ,{field:'department', title: '部门名称',width: 140}
      ,{field:'user_name', title: '填报人姓名',width: 140}
      ,{field:'total_day', title: '天数总和',width: 125}
      ,{field:'traffic_money', title: '交通费总和', width: 125}
      ,{field:'hotel_money', title: '住宿费总和',width: 125}
      ,{field:'meal_money', title: '餐饮费总和',width: 125}
      ,{field:'total_money', title: '费用总和',width: 125}
      ,{field:'create_time', title: '创建时间',width: 140}
      ,{field:'', title: '操作', toolbar:'#tab1Bar'}
    ]]
    ,id: 'testReload'
    ,page: true
    ,toolbar:"#tab2Bar"
    ,text: { none: '暂无相关数据' }
  });
  table.on('tool(user)', function (obj) {
    switch(obj.event){
      case 'show':
        location.replace("/general/erp5/public/tripapi/plan/show?id=" + obj.data.id);
        break;
      case 'showapply':
        location.replace("/general/erp5/public/tripapi/travel/index?id=" + obj.data.id);
        break;
      case 'edit':
        location.replace("/general/erp5/public/tripapi/plan/edit?id=" + obj.data.id);
        break;
      case 'del':
        layer.confirm('该操作不可恢复. 确定删除?', {icon: 3, title: '提示'}, function (index) {
          $.ajax({
            type: "POST",
            url: delurl+"?id="+obj.data.id,
            traditional: true, //是否使用传统的方式浅层序列化,若有数组参数或对象参数需要设置true!!!!!!
            dataType:"json",
            success: function(returnData){
              if(returnData.status == 200){
                obj.del();
                layer.close(index);
              }else{
                layer.open({
                  type: 1
                  ,offset: 'auto'
                  ,id: 'layerDemo'
                  ,content: '<div style="padding: 20px 100px;">'+ returnData.msg +'</div>'
                  ,btn: '确定'
                  ,btnAlign: 'c'
                  ,shade: 0
                  ,yes: function(){
                    layer.closeAll();
                  }
                });
              }
            }
          })
        });
        break;
    }
  })
  table.on('toolbar(user)', function (obj) {
    switch(obj.event){
      case 'show':
        location.replace("/general/erp5/public/tripapi/plan/add");
        break;
    }

  })
  var $ = layui.$, active = {
    reload: function(){
      var demoReload = $('#idreload');
      //执行重载
      table.reload('testReload', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
          key: {
            id: demoReload.val()
          }
        }
      }, 'data');
    }
  };

  $('.apply_table .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});